﻿package 
{
	import flash.geom.Point;
	import flash.display.MovieClip;
	import flash.events.Event;

	public class Room extends MovieClip
	{

		var _mobileObjects:Vector.<MobObj > ;
		//private var _mapCoords:Point;
		//private var _maxBounds:Point;
		//private var _minBounds:Point;
		private var _tilesVect:Vector.<Tile > ;
		private var _mapX:int;
		private var _mapY:int;
		private var _player:Player;
		private var _chestCount:int;
		private var _switchCount:int;
		private var _grid

		public function Room(listNum:int, stageWidth:int,stageHeight:int,aX:int,aY:int,p:Player, grid:Grid)
		{
			// constructor code
			//_tiles = new Vector.<Tile>();
			_grid = grid;
			_mapX = aX;
			_mapY = aY;
			var i:int = 0;
			var j:int = 0;
			var _tiles:Array = new Array(Variables.CollumnsPerRoom);

			for (i = 0; i < Variables.CollumnsPerRoom; i++)
			{
				_tiles[i] = new Array(Variables.RowsPerRoom);
			}
			_mobileObjects = new Vector.<MobObj>();
			_tilesVect = new Vector.<Tile>();




			var numEnemy:uint = GameDataStore.sharedInstance.xml.room[listNum].enemy.length();
			var numTiles:uint = GameDataStore.sharedInstance.xml.room[listNum].tile.length();
			var roomXML:XML = GameDataStore.sharedInstance.xml.room[listNum];
			var objWidth:int = stageWidth / Variables.CollumnsPerRoom;
			var objHeight:int = stageHeight / Variables.RowsPerRoom;
			//go through the XML file and set any tiles that aren't the default(dirt)
			for (i = 0; i < numTiles; i++)
			{
				//store values from XML file
				var tileType:String = roomXML.tile[i].@type;
				var tileX:uint = roomXML.tile[i].@X;
				var tileY:uint = roomXML.tile[i].@Y;
				var sizeX:uint = roomXML.tile[i].@width;
				var sizeY:uint = roomXML.tile[i].@height;
				//check which type of tile then create it, passing in the proper variables
			


			for (j = tileX; j < tileX + sizeX; j++)
			{
				
				for (var k:int = tileY; k < tileY + sizeY; k++)
				{
					switch (tileType)
					{
						case "Dirt" :
							_tiles[j][k] = new DirtTile(objWidth*j,objHeight*k,objWidth,objHeight);
							addChild(_tiles[j][k]);
							break;

						case "Block" :
							_tiles[j][k] = new Obstacle(objWidth*j,objHeight*k,objWidth,objHeight);
							addChild(_tiles[j][k]);
							break;

						case "Pit" :
							_tiles[j][k] = new PitTile(objWidth*j,objHeight*k,objWidth,objHeight,p);
							addChild(_tiles[j][k]);
							break;

						case "Ice" :
							_tiles[j][k] = new IceTile(objWidth*j,objHeight*k,objWidth,objHeight);
							addChild(_tiles[j][k]);
							break;

						case "Crack" :
							_tiles[j][k] = new CrackedBlock(objWidth*j,objHeight*k,objWidth,objHeight);
							addChild(_tiles[j][k]);
							break;

						case "Switch" :
							_tiles[j][k] = new SwitchTile(objWidth*j,objHeight*k,objWidth,objHeight,_grid.SwitchCount,p);
							addChild(_tiles[j][k]);
							_grid.SwitchCount++;
							break;

						case "Chest" :
							_tiles[j][k] = new ChestBlock(objWidth*j,objHeight*k,objWidth,objHeight,_grid.ChestCount,p);
							addChild(_tiles[j][k]);
							_grid.ChestCount++;
							break;

						case "Lock" :
							_tiles[j][k] = new LockBlock(objWidth*j,objHeight*k,objWidth,objHeight,p);
							addChild(_tiles[j][k]);
							break;

						case "BigLock" :
							_tiles[j][k] = new BigLockBlock(objWidth*j,objHeight*k,objWidth,objHeight,p);
							addChild(_tiles[j][k]);
							break;

						case "Bomb" :
							_tiles[j][k] = new BombBlock(objWidth*j,objHeight*k,objWidth,objHeight);
							addChild(_tiles[j][k]);
							break;

						case "IceBlock" :
							_tiles[j][k] = new IceBlock(objWidth*j,objHeight*k,objWidth,objHeight);
							addChild(_tiles[j][k]);
							break;
					}
				}
			}
			}



			//loop through all the tiles and set anything thats null to the default tile(dirt)
			for (i = 0; i < Variables.CollumnsPerRoom; i++)
			{
				for (j = 0; j < Variables.RowsPerRoom; j++)
				{
					if (_tiles[i][j] == null)
					{
						//trace("OH GOD WHY");
						_tiles[i][j] = new Obstacle(objWidth*i,objHeight*j,objWidth,objHeight);
						addChild(_tiles[i][j]);
						//trace(i+","+j);
						//trace(Variables.CollumnsPerRoom);
					}
					_tilesVect.push(_tiles[i][j]);
				}

			}

			for (i = 0; i < numEnemy; i++)
			{

				//store values from XML file
				var enemyX:uint = roomXML.enemy[i]. @ X;
				var enemyY:uint = roomXML.enemy[i]. @ Y;

				_mobileObjects.push(new Enemy(objWidth*enemyX,objHeight*enemyY,objWidth,objHeight,_tilesVect));
			}
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			addEventListener(Event.REMOVED_FROM_STAGE,removedFromStage);
			//trace("WTF");
		}


		public function get tiles2():Vector.<Tile > 
		{
			return _tilesVect;
		}

		public function get mapY():int
		{
			return _mapY;
		}
		public function get mapX():int
		{
			return _mapX;
		}
		public function set player(p:Player):void
		{
			_player = p;
		}

		public function onAddedToStage(e:Event)
		{
			for each (var thing:MobObj in _mobileObjects)
			{
				thing.currentRoom = this;
				if (thing is Enemy)
				{
					(thing as Enemy).player = _player;
				}
				addChild(thing);
			}
			_player.currentRoom = this;
			_player.tiles = this.tiles2;
			addChild(_player);
			//addChild for every mobile object
			//this will add them to the top of the z index

			//check for the XML document, 
			//---------------------make sure the project folder is in the thawed drive if you do this on the lab computers-------------


		}

		public function removedFromStage(e:Event)
		{
			removeChild(_player);
		}

		public function Update()
		{
			for each (var thing:MobObj in _mobileObjects)
			{
				if (thing is Enemy)
				{
					(thing as Enemy).Update();
				}
				thing.Update();
			}
			_player.Update();
		}

	}
}